//
//  XDFilterView.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/7.
//  Copyright © 2017年 East. All rights reserved.
//

#import "XDFilterView.h"
#import "UIColor+XDHex.h"
#import "XDFilterViewTCell.h"

@interface XDFilterView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) UITableView *tableView;

@end

static NSString *const kReUseId = @"XDFilterViewTCell";

@implementation XDFilterView

- (instancetype)initWithTitles:(NSArray *)titles delegate:(id)delegate {
    self = [self init];
    if (self) {
        _titles = titles;
        _delegate = delegate;
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    self.frame = [UIScreen mainScreen].bounds;
    self.alpha = 0;
    
    CGFloat width = CGRectGetWidth([UIScreen mainScreen].bounds) * 0.4;
    CGFloat height = self.titles.count * 44.0;
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    UINib *nib = [UINib nibWithNibName:kReUseId bundle:nil];
    [tableView registerNib:nib forCellReuseIdentifier:kReUseId];
    tableView.tableFooterView = [UIView new];
    tableView.backgroundColor = [UIColor xd_colorWithHex:0x252B3D];
    tableView.scrollEnabled = NO;
    
    self.tableView = tableView;
    [self addSubview:self.tableView];
}

- (void)showInView:(UIView *)view {
    if (self.superview) {
        return;
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    CGRect viewFrame = [view convertRect:view.bounds toView:nil];
    __block CGRect frame = self.tableView.frame;
    frame.origin.x = CGRectGetMinX(viewFrame) + (CGRectGetWidth(viewFrame) - CGRectGetWidth(frame)) / 2.0 ;
    frame.origin.y = CGRectGetMaxY(viewFrame);
    self.tableView.frame = frame;
    
    // 选中某一个cell
    [self.tableView reloadData];
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedIndex inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
    
    [UIView animateWithDuration:0.2 animations:^{
        //        frame.origin.x = CGRectGetMinX(viewFrame) - self.imageViewWidth;
        //        frame.size.width = self.imageViewWidth;
        //        self.bgImageView.frame = frame;
        self.alpha = 1;
    } completion:^(BOOL finished) {
        ;
    }];
}

- (void)hideWithAnimation:(BOOL)animate {
    CGFloat duration = animate ? 0.2 : 0.0;
    
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:duration animations:^{
        //        CGRect frame = weakSelf.bgImageView.frame;
        //        frame.origin.x = CGRectGetMaxX(frame);
        //        frame.size.width = 0;
        //        weakSelf.bgImageView.frame = frame;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

- (void)dismiss {
    [self hideWithAnimation:YES];
}

//- (void)buttonTapped:(UIButton *)button {
//    if ([self.delegate respondsToSelector:@selector(eTChSeaSelView:clickedButtonAtIndex:)]) {
//        [self.delegate eTChSeaSelView:self clickedButtonAtIndex:button.tag];
//    }
//    [self hideWithAnimation:YES];
//}

//- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
//
//    //    for (UIButton * button in self.buttonArray) {
//    //        if ([button pointInside:point withEvent:event]) {
//    //            return button;
//    //        }
//    //    }
//
//    UIView * hitView = [super hitTest:point withEvent:event];
//    if ([hitView isKindOfClass:[UIButton class]]) {
//        return hitView;
//    }
//
//    [self hideWithAnimation:YES];
//    return nil;
//}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self hideWithAnimation:YES];
}

#pragma mark - <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedIndex = indexPath.row;
    if ([self.delegate respondsToSelector:@selector(xDFilterView:clickedButtonAtIndex:)]) {
        [self.delegate xDFilterView:self clickedButtonAtIndex:indexPath.row];
    }
    [self hideWithAnimation:YES];
}

#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XDFilterViewTCell *cell = [tableView dequeueReusableCellWithIdentifier:kReUseId forIndexPath:indexPath];
    cell.centerLabel.text = self.titles[indexPath.row];
    cell.cellBgColor = self.cellBgColor;
    cell.cellMaskColor = self.cellMaskColor;
    return cell;
}

@end
